package cn.jtfadmin.core.modules.sys.controller;


import cn.jtfadmin.base.lang.common.domain.dto.ResultDTO;
import cn.jtfadmin.base.lang.config.user.UserHolder;
import cn.jtfadmin.core.constant.UrlConstant;
import cn.jtfadmin.core.modules.sys.service.SysLogActionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


/**
 * 后管-后台管理登录控制器
 *
 * @author jtf
 * @since 1.0
 */
@RestController
public class SysLoginController {


    @Autowired
    private SysLogActionService sysLogActionService;

    /**
     * 登录生成文档使用,实际退出逻辑在SysSecurityConfig
     *
     * @param username 用户名
     * @param password 密码
     * @return token
     */
    @RequestMapping(value = UrlConstant.SYS_LOGIN_URL, method = RequestMethod.POST)
    public ResultDTO<String> login(@RequestParam() String username, @RequestParam() String password) {
        sysLogActionService.logLoginSuccess(UserHolder.getUserDetails());
        return ResultDTO.successData(UserHolder.getToken());
    }

    /**
     * 退出登录 生成文档使用,实际退出逻辑在SysSecurityConfig
     *
     * @return 返回结果
     */
    @RequestMapping(value = UrlConstant.SYS_LOGOUT)
    public ResultDTO<String> logout() {
        sysLogActionService.logLoginout(UserHolder.getUserDetails());
        return ResultDTO.successData("退出登录成功！");
    }


}
